require.config({
    baseUrl: '../js/library', // 基础路径
    paths: {
        jquery: 'jquery',
        axios: 'axios',
        cookie: 'cookie'
    },
    shim: {}
});

require(['cookie', 'axios', 'jquery'], function (cookie, axios, $) {
    let shop = cookie.get('shop');
    if (shop != undefined) {
        shop = JSON.parse(shop);
        let idList = shop.map(el => el.id).join(',');

        axios
            .get('../shop/all_ids', { params: { idList } })
            .then(res => {
                let data = res.data;
                let tempstring = '';
                data.forEach(el => {
                    let current = shop.filter(elm => elm.id == el.id);

                    tempstring += `
                         <li class="clear_fix" data-id=${el.id}>
                            <div>
                                <input class="box" type="checkbox" />
                                <img src="../${el.imgmin}" alt="" />
                            </div>
                            <div>
                                <a href="#">${el.name} ${JSON.parse(el.edition)[0]} ${JSON.parse(el.color)[0]}</a>
                            </div>
                            <div class="danjia">${el.price}元</div>
                            <div>
                                <div>
                                    <button class="jian">-</button>
                                    <input type="text" value="${current[0].num}" />
                                    <button class="jia">+</button>
                                </div>
                            </div>
                            <div class="zongjia">${el.price * current[0].num}元</div>
                            <div>
                                <button>×</button>
                            </div>
                        </li>`;
                });
                $('#nr').html(tempstring);
                jzwb();
            })
            .catch(err => {
                console.log(err);
            });
    }

    function jzwb() {
        tongji();
        $('#nr>li>div>button').on('click', function () {
            $(this).parent().parent().detach();
            tongji();
        });
        $('#nr>li>div .jian').on('click', function () {
            if (parseInt($(this).parent().children('input').val()) > 1) {
                $(this)
                    .parent()
                    .children('input')
                    .val($(this).parent().children('input').val() - 1);
            }
            $(this)
                .parent()
                .parent()
                .parent()
                .children('.zongjia')
                .html(
                    parseInt($(this).parent().parent().parent().children('.danjia').html()) *
                        parseInt($(this).parent().children('input').val()) +
                        '元'
                );
            tongji();
        });
        $('#nr>li>div .jia').on('click', function () {
            $(this)
                .parent()
                .children('input')
                .val(parseInt($(this).parent().children('input').val()) + 1);
            $(this)
                .parent()
                .parent()
                .parent()
                .children('.zongjia')
                .html(
                    parseInt($(this).parent().parent().parent().children('.danjia').html()) *
                        parseInt($(this).parent().children('input').val()) +
                        '元'
                );
            tongji();
        });
        $('.box').on('click', function () {
            let flag = true;
            $('.box').each((i, e) => {
                if ($(e).prop('checked') == false) {
                    flag = false;
                }
            });
            $('#sumbox').prop('checked', flag);
            tongji();
        });
        $('#sumbox').on('click', function () {
            $('.box').each((i, e) => {
                $(e).prop('checked', $(this).prop('checked'));
            });
            tongji();
        });
        function tongji() {
            let summax = 0;
            let sumxz = 0;
            let sum = 0;
            cookie.remove('shop');
            $('#nr>li').each((i, e) => {
                addItem($(e).attr('data-id'), $(e).children('div').eq(3).children().children('input').val());
                summax += parseInt($(e).children('div').eq(3).children().children('input').val());
                if ($(e).children('div').eq(0).children('.box').prop('checked')) {
                    sum = sum + parseInt($(e).children('.zongjia').html());
                    sumxz += parseInt($(e).children('div').eq(3).children().children('input').val());
                }
            });
            $('#sumjg').html(sum);
            $('#sumsps').html(summax);
            $('#xzsps').html(sumxz);
        }
        function addItem(id, num) {
            let shop = cookie.get('shop');
            let product = {
                id,
                num
            };
            if (shop) {
                shop = JSON.parse(shop);
                if (shop.some(el => el.id === id)) {
                    let _index = shop.findIndex(elm => elm.id == id);
                    let count = parseInt(shop[_index].num);
                    count += parseInt(num);
                    shop[_index].num = count;
                } else {
                    shop.push(product);
                }
            } else {
                shop = [];
                shop.push(product);
            }

            cookie.set('shop', JSON.stringify(shop), 3);
        }
    }
});
